package org.lx.nursing.util;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import org.lx.nursing.domain.*;
import org.lx.nursing.dto.CheckInApplyDto;
import org.lx.nursing.dto.CheckInElderDto;
import org.lx.nursing.mapper.CheckInConfigMapper;
import org.lx.nursing.mapper.CheckInMapper;
import org.lx.nursing.mapper.ContractMapper;
import org.lx.nursing.mapper.ElderMapper;
import org.lx.nursing.service.impl.CheckInServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * 申请入住工具类
 *
 * @see CheckInServiceImpl
 */
@Component
public class CheckInUtil {
    @Resource
    private CheckInConfigMapper checkInConfigMapper;

    @Resource
    private CheckInMapper checkInMapper;

    @Resource
    private ContractMapper contractMapper;

    @Resource
    private ElderMapper elderMapper;

    /**
     * 新增入住配置
     *
     * @param checkInId 入住id
     * @param dto       入住请求信息
     */
    public void insertCheckInConfig(Long checkInId, CheckInApplyDto dto) {
        CheckInConfig checkInConfig = new CheckInConfig();
        BeanUtils.copyProperties(dto.getCheckInConfigDto(), checkInConfig);
        checkInConfig.setCheckInId(checkInId);
        checkInConfigMapper.insert(checkInConfig);
    }

    /**
     * 新增入住信息
     *
     * @param elder 老人信息
     * @param dto   入住请求信息
     * @return 入住信息
     */
    public CheckIn insertCheckIn(Elder elder, CheckInApplyDto dto) {
        CheckIn checkIn = CheckIn.builder()
                .elderId(elder.getId())
                .elderName(elder.getName())
                .idCardNo(elder.getIdCardNo())
                .nursingLevelName(dto.getCheckInConfigDto().getNursingLevelName())
                .startDate(dto.getCheckInConfigDto().getStartDate())
                .endDate(dto.getCheckInConfigDto().getEndDate())
                .bedNumber(elder.getBedNumber())
                .status(0)
                .build();
        checkIn.setRemark(JSON.toJSONString(dto.getElderFamilyDtoList()));
        checkInMapper.insert(checkIn);
        return checkIn;
    }

    /**
     * 新增合同信息
     *
     * @param contractNo 合同id
     * @param elder      老人信息
     * @param dto        入住请求信息
     */
    public void insertContract(String contractNo, Elder elder, CheckInApplyDto dto) {
        LocalDateTime start = dto.getCheckInConfigDto().getStartDate();
        LocalDateTime end = dto.getCheckInConfigDto().getEndDate();
        Integer status = start.isBefore(LocalDateTime.now()) ? 0 : 1;
        Contract contract = new Contract();
        BeanUtils.copyProperties(dto.getCheckInContractDto(), contract);
        contract.setContractNumber(contractNo);
        contract.setElderId(elder.getId());
        contract.setElderName(elder.getName());
        contract.setStatus(status);
        contract.setStartDate(start);
        contract.setEndDate(end);
        contractMapper.insert(contract);
    }

    /**
     * 新增或更新老人信息
     *
     * @param bed 床位
     * @param dto 老人信息
     * @return 老人信息
     */
    public Elder insertOrUpdate(Bed bed, CheckInElderDto dto) {
        Elder elder = new Elder();
        BeanUtils.copyProperties(dto, elder);
        elder.setBedNumber(bed.getBedNumber());
        elder.setBedId(bed.getId());
        elder.setStatus(1);
        LambdaQueryWrapper<Elder> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Elder::getIdCardNo, dto.getIdCardNo()).ne(Elder::getStatus, 1);
        Elder eld = elderMapper.selectOne(wrapper);
        if (ObjectUtils.isNotEmpty(eld)) elderMapper.updateById(elder);
        else elderMapper.insert(elder);
        return elder;
    }
}
